package com.learn;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;

import javax.swing.*;

public class MainWindow extends JFrame implements ActionListener {
	JMenuBar menubar;
	JMenu menu;
	JMenuItem[] menuitems;
	JLabel label;
	JPanel panel;
	String[] menuitemsnames = { "录入学生信息", "修改学生信息", "查询学生信息", "删除学生信息", "查看所有学生信息" };
	CardLayout cardlayout;
	InputWindow inputwindow;
	ModifyWindow modifywindow;
	DelWindow delwindow;
	SearchWindow searchwindow;
	SearchAllWindow searchallwindow;

	public MainWindow() throws SQLException {
		init();
		this.setLocation(350, 150);
		setSize(800, 600);
		setVisible(true);
		setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	public void init() throws SQLException {
		menubar = new JMenuBar();
		menu = new JMenu("操作菜单");
		menu.setFont(new Font("Serif", Font.PLAIN, 22));
		menuitems = new JMenuItem[5];
		for (int i = 0; i < 5; i++) {
			menuitems[i] = new JMenuItem(menuitemsnames[i]);
			menuitems[i].setFont(new Font("宋体", Font.PLAIN, 18));
			menuitems[i].addActionListener(this);// 注册监听器
			menu.add(menuitems[i]);
		}
		menubar.add(menu);
		setJMenuBar(menubar);
		panel = new JPanel();
		//必须设置为透明的。否则看不到图片
		panel.setOpaque(false);
		cardlayout = new CardLayout();
		label = new JLabel("欢迎使用社团管理系统", JLabel.CENTER);
		label.setFont(new Font("隶书", Font.BOLD, 40));
		label.setForeground(Color.blue);
		panel.setLayout(cardlayout);
		panel.add("welcome", label);

		inputwindow = new InputWindow();
		panel.add("input", inputwindow);

		modifywindow = new ModifyWindow();
		panel.add("modify", modifywindow);

		delwindow = new DelWindow();
		panel.add("del", delwindow);

		searchwindow = new SearchWindow();
		panel.add("search", searchwindow);

		searchallwindow = new SearchAllWindow();
		panel.add("all", searchallwindow);

		panel.setBackground(Color.WHITE);
		add(panel, BorderLayout.CENTER);
	}

	public static void main(String[] args) throws SQLException {
		new MainWindow();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equals("录入学生信息")) {
			cardlayout.show(panel, "input");
		} else if (arg0.getActionCommand().equals("修改学生信息")) {
			cardlayout.show(panel, "modify");
		} else if (arg0.getActionCommand().equals("删除学生信息")) {
			cardlayout.show(panel, "del");
		} else if (arg0.getActionCommand().equals("查询学生信息")) {
			cardlayout.show(panel, "search");
		} else if (arg0.getActionCommand().equals("查看所有学生信息")) {
			cardlayout.show(panel, "all");
		}

	}

}
